//
//  CustomTableViewCell.m
//  Demo
//
//  Created by iOS on 2017/5/24.
//  Copyright © 2017年 MS. All rights reserved.
//

#import "CustomTableViewCell.h"
#import <Masonry.h>
#import "Goods.h"

@interface CustomTableViewCell ()

@end

@implementation CustomTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+ (CustomTableViewCell *)cellWithTableView:(UITableView *)tableView
{
    static NSString *reuseIdentifier = @"EditSortingTableViewCell";
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
    if (!cell)
    {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    
    cell.backgroundColor = [UIColor whiteColor];
    UIView *whiteView = [[UIView alloc] init];
    whiteView.backgroundColor = [UIColor whiteColor];
    cell.selectedBackgroundView = whiteView;
    
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self.contentView addSubview:self.selectedImgView];
        [self.contentView addSubview:self.imgView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.numOfGoodsLabel];
        [self.contentView addSubview:self.monthlySalesLabel];
        [self.contentView addSubview:self.priceLabel];
        [self.contentView addSubview:self.moveToTopImgView];
        [self.contentView addSubview:self.longPressMoveImgView];
    }
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [_selectedImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    [_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.equalTo(_selectedImgView.mas_right).offset(8);
        make.centerY.equalTo(self.contentView);
        make.width.equalTo(_imgView.mas_height);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(8);
        make.left.equalTo(_imgView.mas_right).offset(8);
    }];
    
    [_numOfGoodsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_bottom).offset(8);
        make.left.equalTo(_imgView.mas_right).offset(8);
    }];
    
    [_monthlySalesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_bottom).offset(8);
        make.left.equalTo(_numOfGoodsLabel.mas_right).offset(8);
    }];
    
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_numOfGoodsLabel.mas_bottom).offset(8);
        make.left.equalTo(_imgView.mas_right).offset(8);
    }];
    
    [_moveToTopImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(_longPressMoveImgView.mas_left).offset(-10);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    [_longPressMoveImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.mas_equalTo(-15);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
}


#pragma mark - lazy loading

- (UIImageView *)selectedImgView
{
    if (!_selectedImgView)
    {
        _selectedImgView = [[UIImageView alloc] init];
        _selectedImgView.image = [UIImage imageNamed:@"img"];
    }
    
    return _selectedImgView;
}

- (UIImageView *)imgView
{
    if (!_imgView)
    {
        _imgView = [[UIImageView alloc] init];
        _imgView.contentMode = UIViewContentModeScaleAspectFit;
        _imgView.image = [UIImage imageNamed:@"img"];
    }
    
    return _imgView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = @"菜品";
        _titleLabel.textColor = [UIColor darkTextColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:18];
    }
    
    return _titleLabel;
}

- (UILabel *)numOfGoodsLabel
{
    if (!_numOfGoodsLabel)
    {
        _numOfGoodsLabel = [[UILabel alloc] init];
        _numOfGoodsLabel.text = @"库存无限";
        _numOfGoodsLabel.textColor = [UIColor lightTextColor];
        _numOfGoodsLabel.font = [UIFont systemFontOfSize:13];
    }
    
    return _numOfGoodsLabel;
}

- (UILabel *)monthlySalesLabel
{
    if (!_monthlySalesLabel)
    {
        _monthlySalesLabel = [[UILabel alloc] init];
        _monthlySalesLabel.text = @"月售 0";
        _monthlySalesLabel.textColor = [UIColor lightTextColor];
        _monthlySalesLabel.font = [UIFont systemFontOfSize:13];
    }
    
    return _monthlySalesLabel;
}

- (UILabel *)priceLabel
{
    if (!_priceLabel)
    {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.text = [NSString stringWithFormat:@"¥ %.1f", 10.0];
        _priceLabel.textColor = [UIColor redColor];
        _priceLabel.font = [UIFont boldSystemFontOfSize:18];
    }
    
    return _priceLabel;
}

- (UIImageView *)moveToTopImgView
{
    if (!_moveToTopImgView)
    {
        _moveToTopImgView = [[UIImageView alloc] init];
        _moveToTopImgView.image = [UIImage imageNamed:@"img"];
//        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moveToTopAction:)];
//        [_moveToTopImgView addGestureRecognizer:tapGestureRecognizer];
//        _moveToTopImgView.userInteractionEnabled = YES;
    }
    
    return _moveToTopImgView;
}

- (UIImageView *)longPressMoveImgView
{
    if (!_longPressMoveImgView)
    {
        _longPressMoveImgView = [[UIImageView alloc] init];
        _longPressMoveImgView.image = [UIImage imageNamed:@"GoodsEdit"];
        
//        UILongPressGestureRecognizer *longPresssGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(processLongPressGesture:)];
//        longPresssGestureRecognizer.minimumPressDuration = 0.35f;
//        [_longPressMoveImgView addGestureRecognizer:longPresssGestureRecognizer];
//        _longPressMoveImgView.userInteractionEnabled = YES;
    }
    
    return _longPressMoveImgView;
}


- (void)configCellWithModel:(Goods *)model indexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        _moveToTopImgView.hidden = YES;
    }
    else
    {
        _moveToTopImgView.hidden = NO;
    }
    
    _titleLabel.text = model.name;
    _numOfGoodsLabel.text = [NSString stringWithFormat:@"库存 %ld", model.num];
    _monthlySalesLabel.text = [NSString stringWithFormat:@"月售 %ld", model.monthlySales];
    _priceLabel.text = [NSString stringWithFormat:@"¥ %.1f", model.price];
    
    [self layoutIfNeeded];
}

- (CGFloat)heightForRow
{
    return CGRectGetMaxY(_priceLabel.frame) + 8;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
